﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Dome_Update
{
    public partial class MyListView : ListView
    {
        public MyListView()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.OwnerDraw = true;
            this.View = View.Details;
            ProgressColor = Color.LightBlue;
            ProgressTextColor = Color.Black;
            ProgressIndex = 2;
        }
        public int ProgressIndex { get; set; }
        public Color ProgressColor { get; set; }
        public Color ProgressTextColor { get; set; }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {

            e.DrawDefault = true;

            base.OnDrawColumnHeader(e);
        }


        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {

            if (e.ColumnIndex != this.ProgressIndex)
            {
                e.DrawDefault = true;
                base.OnDrawSubItem(e);
            }
            else
            {
                float per = float.Parse(e.Item.SubItems[e.ColumnIndex].Text);

                per = (per / 100);

                Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
                DrawProgress(rect, per, e.Graphics);

            }

        }
        //C# listview进度条显示

        ///绘制进度条列的subitem  

        private void DrawProgress(Rectangle rect, float percent, Graphics g)
        {

            if (rect.Height > 4 && rect.Width > 2)
            {

                if ((rect.Top > 0 && rect.Top < this.Height) && (rect.Left > this.Left && rect.Left < this.Width))
                {

                    //绘制进度  

                    int width = (int)(rect.Width * percent);

                    Rectangle newRect = new Rectangle(rect.Left + 1,
                    rect.Top + 1, width - 2, rect.Height - 4);

                    using (Brush tmpb =
                    new SolidBrush(this.ProgressColor))
                    {

                        g.FillRectangle(tmpb, newRect);

                    }


                    newRect = new Rectangle(rect.Left +
                     1, rect.Top + 1, rect.Width - 2,
                     rect.Height - 4);

                    g.DrawRectangle(Pens.LightBlue, newRect);

                    StringFormat sf = new StringFormat();

                    sf.Alignment = StringAlignment.Center;

                    sf.LineAlignment = StringAlignment.Center;

                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    newRect = new Rectangle(rect.Left + 1,
                    rect.Top + 1, rect.Width - 2,
                    rect.Height - 2);

                    using (Brush b = new SolidBrush(ProgressTextColor))
                    {
                        g.DrawString(
                        percent.ToString("p1"), this.Font, b, newRect, sf);

                    }

                }

            }
            //C# listview进度条显示
            else
            {
                return;

            }

        }

    }

}